<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/admin/home/role'}">职务管理</el-breadcrumb-item>
      <el-breadcrumb-item>职务权限管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索框 -->
    <el-form :inline="true" class="searchForm">
      <el-form-item>
        <el-input v-model="keyword" clearable placeholder="请输入要搜索的关键字" @clear="clear"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="add" plain icon="el-icon-plus">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="content">
      <el-row :gutter="0">
        <el-col v-for="item in dataList" :key="item.id" :span="4" :offset="0">

          <el-popover placement="top-start" :title="item.type=='menu'? '菜单权限':'资源权限'" width="200" trigger="hover" :content="item.menuKey">
            <el-tag closable slot="reference" v-if="item.type=='menu'" type="success" size="normal" effect="dark" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else-if="item.type=='power' && item.menuKey.indexOf('_R')>0" type="success" size="normal" effect="light" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else-if="item.type=='power' && item.menuKey.indexOf('_C')>0" type="info" size="normal" effect="light" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else-if="item.type=='power' && item.menuKey.indexOf('_U')>0" type="warning" size="normal" effect="light" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else-if="item.type=='power' && item.menuKey.indexOf('_CRUD')>0" type="success" size="normal" effect="plain" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else-if="item.type=='power' && item.menuKey.indexOf('_D')>0" type="danger" size="normal" effect="light" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
            <el-tag closable slot="reference" v-else type="success" size="normal" effect="plain" @click="openDetail(item)" @close="remove(item)">{{item.menuName}}</el-tag>
          </el-popover>

        </el-col>
      </el-row>
    </div>

    <!-- 分页组件 -->
    <el-pagination :current-page="page.page" :total="page.total" :page-size="page.limit" background layout="prev, pager, next" @current-change="currentChange">
    </el-pagination>

    <!-- 详情对话框 -->
    <el-dialog title="权限详情" :visible.sync="dialogEdit" center v-loading="loading">
      <el-form class="form" :model="form" label-left label-width="100px" :rules="rules" ref="editForm">
        <el-form-item label="权限名称" prop="menuName">
          <el-input readonly v-model.trim="form.menuName"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="menuKey">
          <el-input readonly v-model.trim="form.menuKey"></el-input>
        </el-form-item>
        <el-form-item label="路由地址" v-show="form.type==='menu'">
          <el-input readonly v-model.trim="form.path"></el-input>
        </el-form-item>
        <el-form-item label="图标" v-show="form.type==='menu'">
          <el-input readonly v-model.trim="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch disabled v-model="form.status" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </el-form-item>
        <el-form-item label="类型">
          <el-radio readonly :value="form.type" label="power">资源权限</el-radio>
          <el-radio readonly :value="form.type" label="menu">菜单权限</el-radio>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog class="add-dialog" title="选择权限" :visible.sync="dialogAdd" center v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="8" :offset="8">
          <el-select class="select" no-data-text="已拥有全部权限" v-model="select" placeholder="请选择" multiple collapse-tags filterable>
            <el-option-group v-for="group in menuAll" :key="group.menuName" :label="group.menuName">
              <el-option v-for="item in group.list" :key="item.id" :label="item.menuName" :value="item.id">
              </el-option>
            </el-option-group>
          </el-select>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="doAdd('addForm')">确 定</el-button>
        <el-button @click="dialogAdd = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  adminRoleMenuListApi as getListApi,
  adminEditRoleMenuApi as editApi,
  adminAddRoleMenuApi as addApi,
  adminDelRoleMenuApi as delApi,
  adminDelsRoleMenuApi as delsApi,
  adminMenuAllApi,
} from "@/api/admin.js";
export default {
  inject: ["reload"],
  data() {
    return {
      //职务id
      role_id: 0,
      //表格展示数据
      dataList: [],
      //分页数据
      page: {
        page: 1,
        limit: 18,
        total: 0,
      },
      //选择的数据
      select: [],
      //加载
      loading: false,
      //表单数据
      form: {}, //表单数据
      keyword: "", //关键字
      dialogEdit: false, //显示编辑对话款
      dialogAdd: false, //显示编辑对话款
      //表单校验
      rules: {
        roleName: {
          required: true,
          message: "请输入职务名称",
          trigger: "blur",
        },
        roleKey: { required: true, message: "请输入职务标识", trigger: "blur" },
      },
      menuAll: null, //所有权限列表
    };
  },
  methods: {
    //获取数据列表
    getList(
      page = this.page.page,
      limit = this.page.limit,
      keyword = this.keyword,
      id = this.role_id
    ) {
      let queryParam = {
        page,
        limit,
        keyword,
        id,
      };
      getListApi(queryParam)
        .then((res) => {
          this.loading = true;
          const result = res.data;
          if (result.status === 200) {
            const dataList = result.data.content;
            //将数status中的1和0 转换为true 和false
            dataList.map((role) => {
              role.status = role.status == 1 ? true : false;
              return role;
            });
            this.dataList = dataList;
            this.page.total = result.data.total;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //分页
    currentChange(e) {
      this.page.page = e;
      this.getList();
    },
    //执行编辑操作
    doEdit(e) {
      this.$refs[e].validate((valid) => {
        if (valid) {
          this.form.status = this.form.status ? 1 : 0;
          editApi(this.form)
            .then((res) => {
              this.loading = true;
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("修改成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.loading = false;
              this.dialogEdit = false;
              this.reload();
            });
        }
      });
    },
    //打开添加对话款
    add() {
      adminMenuAllApi({ id: this.role_id }).then((res) => {
        const result = res.data;
        if ((result.status = 200)) {
          const data = result.data;
          this.menuAll = [
            { menuName: "根菜单权限", list: data.rootList },
            { menuName: "二级菜单权限", list: data.menuList },
            { menuName: "资源权限", list: data.powerList },
          ];
        }
      });
      this.dialogAdd = true;
    },
    //执行添加操作
    doAdd(e) {
      addApi({ roleId: this.role_id, menuIdList: this.select })
        .then((res) => {
          this.loading = true;
          const result = res.data;
          if (result.status == 200) {
            this.$message.success("添加成功");
          } else {
            this.$message.error(result.msg);
          }
        })
        .finally(() => {
          this.loading = false;
          this.dialogAdd = false;
          this.reload();
        });
    },
    //删除
    remove(e) {
      this.$confirm("确认要删除吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          delApi({ roleId: this.role_id, menuId: e.id })
            .then((res) => {
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.reload();
            });
        })
        .catch(() => {
          return;
        });
    },
    //批量删除
    dels() {
      const ids = this.select.map((role) => {
        return role.id;
      });
      const data = { ids };
      this.$confirm("确认要删除吗？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          delsApi(data)
            .then((res) => {
              const result = res.data;
              if (result.status == 200) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(result.msg);
              }
            })
            .finally(() => {
              this.reload();
            });
        })
        .catch(() => {
          return;
        });
    },
    //搜索
    search() {
      this.getList();
    },
    //清空文本框
    clear() {
      this.getList();
    },
    //详情页面
    openDetail(e) {
      this.dialogEdit = true;
      this.form = e;
    },
  },
  created() {
    this.role_id = this.$route.query.id;
    this.getList();
  },
};
</script>

<style  scoped>
.el-tabs {
  margin-top: 50px;
}
.el-pagination {
  float: right;
  margin: 20px;
}
.searchForm {
  margin-left: 100px;
  margin-top: 20px;
}
.form {
  padding: 20px 50px;
}
.form .el-form-item {
  width: 300px;
  margin: 20px auto;
}
.el-tag {
  width: 120px;
  text-align: center;
}
.content {
  border: 1px solid #eee;
  padding: 20px;
}
.content .el-tag {
  margin: 20px 0;
}
.el-tag:hover {
  transform: scale(1.2);
  cursor: pointer;
}
.select {
  margin: 0 auto;
}
.el-row {
  margin: 20px 0;
}
</style>